package terra.twl.window;

import terra.Loader;
import terra.network.packet.client.RequestDropItem;
import terra.slick.GameState;
import terra.twl.widget.ItemSlot;
import de.matthiasmann.twl.Button;
import de.matthiasmann.twl.EditField;
import de.matthiasmann.twl.ResizableFrame;


public class CountChooser extends ResizableFrame {
	
	IDropable to,from;
	ItemSlot item;
	EditField editfield;
	Button ok, cancel,delete,
	zero,one,two,three,four,five,six,seven,eight,nine;
	
	int count;
	private int mx;
	private int my;
	public CountChooser(){
		addCloseCallback(new Cancel());
		editfield = new EditField();
		editfield.setMaxTextLength(7);
		
		ok = new Button("Ok");
		ok.setSize(18, 16);
		ok.addCallback(new Apply());
		cancel = new Button("Cancel");
		cancel.setSize(42, 16);
		cancel.addCallback(new Cancel());
		delete = new Button("Del");
		delete.setSize(20,16);
		delete.addCallback(new Appender(-1));
		
		zero = new Button("0");
		zero.setSize(16, 16);
		zero.addCallback(new Appender(0));
		one = new Button("1");
		one.setSize(16, 16);
		one.addCallback(new Appender(1));
		two = new Button("2");
		two.setSize(16, 16);
		two.addCallback(new Appender(2));
		three = new Button("3");
		three.setSize(16, 16);
		three.addCallback(new Appender(3));
		four = new Button("4");
		four.setSize(16, 16);
		four.addCallback(new Appender(4));
		five = new Button("5");
		five.setSize(16, 16);
		five.addCallback(new Appender(5));
		six = new Button("6");
		six.setSize(16, 16);
		six.addCallback(new Appender(6));
		seven = new Button("7");
		seven.setSize(16, 16);
		seven.addCallback(new Appender(7));
		eight = new Button("8");
		eight.setSize(16, 16);
		eight.addCallback(new Appender(8));
		nine = new Button("9");
		nine.setSize(16, 16);
		nine.addCallback(new Appender(9));
		
		editfield.setPosition(5, 5);
		editfield.setSize(60, 12);
		

		ok.setPosition(5, 30);
		cancel.setPosition(5, 55);
		delete.setPosition(48, 30);
		
		seven.setPosition(75, 5);
		eight.setPosition(98, 5);
		nine.setPosition(121, 5);

		four.setPosition(75, 30);
		five.setPosition(98, 30);
		six.setPosition(121, 30);

		one.setPosition(75, 55);
		two.setPosition(98, 55);
		three.setPosition(121, 55);
		zero.setPosition(53,55);
		
		add(ok);
		add(cancel);
		add(delete);
		add(zero);
		add(editfield);
		
		add(seven);
		add(eight);
		add(nine);
		
		add(four);
		add(five);
		add(six);
		
		add(one);
		add(two);
		add(three);
	}
	
	protected void reset() {
		item = null;
		to = null;
		editfield.setText("");
	}

	@Override
	protected void layout(){
		
	}
	
    @Override
    public int getPreferredInnerWidth() {
        return 120;
    }

    @Override
    public int getPreferredInnerHeight() {
        return 80;
    }

    class Apply implements Runnable{

		@Override
		public void run() {
			if (editfield.getTextLength() == 0)
				return;
			for(int i = 0; i < editfield.getText().length();i++){
				if (!Character.isDigit(editfield.getText().charAt(i))){
					editfield.setText("");
					return;
				}
			}
			if (to != null){
				to.dragStopped(item, mx,my,from,Integer.parseInt(editfield.getText()));
			} else {
    			RequestDropItem request =
    					new RequestDropItem(GameState.getInstance().player.getObjectId(),item.def.getOId(),
    							item.def.getId(),mx,my,Integer.parseInt(editfield.getText()));
    			Loader.handler.send(request);
    			//System.out.println(request.toString());
			}
			reset();
			setVisible(false);
		}
    }
    
    class Cancel implements Runnable{

		@Override
		public void run() {
			reset();
			setVisible(false);
		}
    }
    
    class Appender implements Runnable{

    	public Appender(int n){
    		num = n;
    	}
    	
    	int num;
		@Override
		public void run() {
			if (num == -1 && editfield.getTextLength() > 0)
				editfield.setText(editfield.getText().substring(0, editfield.getTextLength()-1));
			else
				editfield.setText(editfield.getText()+num);
		}
    }

	public void init(ItemSlot slot,int x,int y, IDropable wndTo,IDropable wndFrom) {
		item = slot;
		mx = x;
		my = y;
		to = wndTo;
		from = wndFrom;
		setVisible(true);
		requestKeyboardFocus();
	} 
}
